<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function fn(nums, target) {
            let l = 0;
            let r = nums.length - 1;
            while (l < r) {
                let mid = Math.floor((r - l) / 2)
                if (nums[mid] === target) {
                    return mid
                } else if (nums[mid] < target) {
                    l = mid + 1
                } else {
                    r = mid - 1
                }
            }
        }
        let arr = [1, 2, 3, 4, 5, 6, 7]
        console.log(fn(arr, 4));

    </script>
</body>

</html>